package cn.ddy.crud.controller;

import cn.ddy.crud.beans.User;
import cn.ddy.crud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ddy
 * @create 2020-08-18 18:03:16
 */
@Controller
@RequestMapping("user")
//  @RestController = @Controller + @ResponseBody
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("list")
    public String list(Map<String,Object> map){
        List<User> users = userService.getUsers();
        map.put("users",users);
        return "user/list";
    }

    @GetMapping("del/{id}")
    public Object del(@PathVariable("id") Integer id) {
        userService.delById(id);
        return "redirect:/user/list";
    }

    @GetMapping("jsonList")
    @ResponseBody
    public Object jsonList(Map<String,Object> map){
        List<User> users = userService.getUsers();
        return users;
    }


}
